﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_InsertPerson : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string typeofUser = Request["typeofAccount"].ToString();
        lbType.Text = typeofUser;
        
        loadForm();
    }
    public void loadForm()
    {
        string typeofAccount = Session["typeofAccount"].ToString();
        if (typeofAccount.Equals("Student"))
        {
            lb_Subject.Visible=false;
            txt_Subject.Visible=false;
            lb_typeofaccount.Visible = false;
            drd_TypeofAccount.Visible = false;
            lb_UserID.Text = setUserIDtoLabel("Std_");
            rqrSubject.Visible = false;
            rqrPhone.Visible = false;
            rqrEmail.Visible = false;
         //   lblRqrSubject.Visible = false;
        }
        else if (typeofAccount.Equals("Staff"))
        {

            lb_Course.Visible = false;
            txtCourse.Visible = false;
            if (drd_TypeofAccount.SelectedValue.ToString().Equals("Staff"))
            {
                lb_UserID.Text = setUserIDtoLabel("Stf_");
                lb_Subject.Visible = true;
                txt_Subject.Visible = true;
                lb_Class.Visible = true;
                txtClass.Visible = true;
                rqrPhone.Visible = false;
                rqrCourse.Visible = false;
            }
            else
            {
                lb_Subject.Visible = false;
                txt_Subject.Visible = false;
                lb_Class.Visible = false;
                txtClass.Visible = false;

                lb_UserID.Text = setUserIDtoLabel("Mng_");
                rqrPhone.Visible = false;
                rqrClass.Visible = false;
                rqrCourse.Visible = false;
                rqrSubject.Visible = false;
            }

        }
        else
        {
            lb_UserID.Text = setUserIDtoLabel("Cus_");
            drd_TypeofAccount.Visible = false;
            lb_typeofaccount.Visible = false;
            lb_Subject.Visible = false;
            txt_Subject.Visible = false;
            lb_Class.Visible = false;
            txtClass.Visible = false;
            lb_Course.Visible = false;
            txtCourse.Visible = false;
            lb_DateJoin.Visible = false;
            txtDateJoin.Visible = false;
            RqrBirthday.Visible = false;
            rqrEmail.Visible = false;
            rqrDateJoin.Visible = false;
            rqrClass.Visible = false;
            rqrCourse.Visible = false;
            rqrSubject.Visible = false;
        }
    }

    public string setUserIDtoLabel(string typeofAccount)
    {
        //UserDAO userDAO = new UserDAO();
        //string userID = userDAO.GetLast_UserID(typeofAccount+"%");
        //string[] temp = userID.Split('_');
        //int num = int.Parse(temp[1].ToString()) + 1;
        //return typeofAccount + num.ToString();
         UserDAO  userDAO = new  UserDAO();
        string  temp1 = "";
        if  (userDAO.GetLast_UserID(typeofAccount + "%") != null)
        {
            string  userID = userDAO.GetLast_UserID(typeofAccount + "%");
            string[] temp = userID.Split('_');
            int  num = int.Parse(temp[1].ToString()) + 1;
            temp1 = (num / 1000).ToString() + ((num % 1000) / 100).ToString() + (((num % 1000) % 100) / 10).ToString() + (((num % 1000) % 100) % 10).ToString();
        }
        else  return  typeofAccount + "0001";
        return typeofAccount + temp1;
    }

    protected void btn_Insert_Click(object sender, EventArgs e)
    {
        UserDAO userDAO = new UserDAO();
        try
        {
            UserInfo userInfo = new UserInfo();
            userInfo.UserID = lb_UserID.Text;
            userInfo.FullName = txtFullName.Text;
            userInfo.BirthDay = txtBirthday.Text;
            userInfo.Address = txtAddress.Text;
            if (radMale.Checked == true)
            {
                userInfo.Gender = "Male";
            }
            else
                userInfo.Gender = "Female";
            userInfo.Phone = txtPhone.Text;
            userInfo.Email = txtEmail.Text;
            if (Session["typeofAccount"].ToString() != "Customer")
                userInfo.DateJoin = txtDateJoin.Text;
            else userInfo.DateJoin = DateTime.Now.ToShortDateString();
            userInfo.St_class = txtClass.Text;
            userInfo.Course = txtCourse.Text;
            userInfo.Subject = txt_Subject.Text;
            if (lbType.Text.Equals("Staff"))
            {
                userInfo.Type = drd_TypeofAccount.SelectedItem.ToString();
            }
            else
            {
                userInfo.Type = lbType.Text;
            }

            //validate

            if (userDAO.Insert(userInfo) > 0)
            {

                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Insert Successfull!');", true);
                Reset();
                loadForm();
            }
            else
            {
                lblResult.Text = "Add Failed!!!";
            }

        }
        catch (Exception ex)
        {
            lblResult.Text = ex.Message;
        }
    }

    public void Reset()
    {
        txt_Subject.Text = "";
        txtAddress.Text = "";
        txtBirthday.Text = "";
        txtClass.Text = "";
        txtCourse.Text = "";
        txtDateJoin.Text = "";
        txtEmail.Text = "";
        txtFullName.Text = "";
        txtPhone.Text = "";
        radMale.Checked = true;
        drd_TypeofAccount.SelectedIndex = 0;
        lblResult.Text = "";
    }
    protected void btn_Reset_Click(object sender, EventArgs e)
    {
        Reset();
    }
}